<?php
defined('JOC') or die;

class Service
{
	public $_module_id 	= 0 ;
	public $_action			= '_read_simple' ;
	public $_result			= Array( 'result' => 'None' , 'msg' => 'No action executed' ) ;
	 
	function __construct( $output = 'json' )
	{
		global $db ;
		
		$this->_module_id = R::all('module_id','int') ;
		$this->_action 		= R::all('action') ;
		$this->_output		= 'json' ;
		
		$sql = " SELECT * from {prefix}module WHERE module_id = {$this->_module_id} LIMIT 1 ; " ;
		
		$module_data = $db->query($sql,false) ;
		
		$module_data = isset($module_data[0]) ? $module_data[0] : Array() ; 
		
		
		
		if($module_data && isset($module_data['class']))
		{
			$module = new $module_data['class']();

			if(method_exists(get_class( $module ), $this->_action))
			{
				$method = $this->_action ;
				
				$this->_result['result'] = $module->$method() ;
				
				if($this->_result['result'])
				{
					$this->_result['msg'] 		= 'Action executed sucessfully.' ;
				}
				else
				{
					$this->_result['result'] = false ;
					$this->_result['msg'] 		= 'The Action cannot be executed sucessfully.' ;
				}
			}
		}
	}
  function print_screen()
  {
    echo  $this->_output == 'json' ? json_encode($this->_result) : $this->_get_xml() ; 
  }
  function _get_xml()
  {
  	//some code here to encode to xml 
  	return "<arrresult>
  						<result>{$this->_result['result']}</result>
  						<msg>{$this->_result['msg']}</msg>
  					</arrresult>" ;
  }
}